#include "setwig.h"
#include "ui_setwig.h"
#include "globalval.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include "mqtt/qmqtt.h"

SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig)
{
    ui->setupUi(this);
    temp = 1;
    temp2 = 1;
    temp3 = 1;
    // 设置半透明背景和金黄色圆弧边框
    QString commonStyle =
        "QPushButton {"
            "background-color: rgba(185, 255, 185, 0.6);" // 半透明背景色
            "color: green;"
            "font-family: '黑体';"
            "font-size: 28px;"
            "border: 4px solid #FFD700;" // 添加金黄色边框
            "border-radius: 15px;"      // 圆角半径
            "padding: 10px;"            // 内边距
        "}"
        "QPushButton:hover {"
            "background-color: rgba(100, 255, 100, 0.6);" // 悬停时的半透明背景色
            "border: 4px solid #FFA500;" // 悬停时橙色边框
        "}";

    ui->pushButton_led1->setStyleSheet(commonStyle);
    ui->pushButton_beep->setStyleSheet(commonStyle);
    ui->pushButton_fan->setStyleSheet(commonStyle);



//    label->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
   // ui->label->setStyleSheet("background-color: rgba(255, 255, 255, 0);color: black;font-family: '黑体'; font-size: 18px;");

    // 设置 QLCDNumber 的 digitCount 为 8
        ui->lcd_time->setDigitCount(8);

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout,this,&SetWig::updateTime);
    timer->start(1000);
    updateTime();
}

SetWig::~SetWig()
{
    delete ui;
}

void SetWig::on_pushButton_led1_clicked()
{
    if(temp == 1)
    {
        ui->pushButton_led1->setText("OFF");
        ui->pushButton_led1->setStyleSheet(
                    "QPushButton {"
                    "background-color: rgba(185, 255, 185, 0.6);" // 半透明背景色
                    "color: rgb(255, 0, 0);"
                        "font-family: '黑体';"
                        "font-size: 28px;"
                        "border: 4px solid #FFD700;" // 添加金黄色边框
                        "border-radius: 15px;"      // 圆角半径
                        "padding: 10px;"            // 内边距
                    "}"
                    "QPushButton:hover {"
                        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
                        "stop:0 rgba(100, 255, 100, 150), stop:1 rgba(185, 255, 185, 150));" // 悬停时的半透明渐变背景
                        "border: 4px solid #FFA500;" // 悬停时橙色边框
                    "}");
        temp = 0;
        QJsonObject object;
        object["led_1"] = 1;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);

    }
    else
    {
        ui->pushButton_led1->setText("ON");
        ui->pushButton_led1->setStyleSheet(
                    "QPushButton {"
                    "background-color: rgba(185, 255, 185, 0.6);" // 半透明背景色
                    "color: green;"
                        "font-family: '黑体';"
                        "font-size: 28px;"
                        "border: 4px solid #FFD700;" // 添加金黄色边框
                        "border-radius: 15px;"      // 圆角半径
                        "padding: 10px;"            // 内边距
                    "}"
                    "QPushButton:hover {"
                        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
                        "stop:0 rgba(100, 255, 100, 150), stop:1 rgba(185, 255, 185, 150));" // 悬停时的半透明渐变背景
                        "border: 4px solid #FFA500;" // 悬停时橙色边框
                    "}");
        temp = 1;
        QJsonObject object;
        object["led_1"] = 0;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
}

void SetWig::on_pushButton_beep_clicked()
{
    if(temp2 == 1)
    {
        ui->pushButton_beep->setText("OFF");
        ui->pushButton_beep->setStyleSheet(
                    "QPushButton {"
                    "background-color: rgba(185, 255, 185, 0.6);" // 半透明背景色
                    "color: rgb(255, 0, 0);"
                        "font-family: '黑体';"
                        "font-size: 28px;"
                        "border: 4px solid #FFD700;" // 添加金黄色边框
                        "border-radius: 15px;"      // 圆角半径
                        "padding: 10px;"            // 内边距
                    "}"
                    "QPushButton:hover {"
                        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
                        "stop:0 rgba(100, 255, 100, 150), stop:1 rgba(185, 255, 185, 150));" // 悬停时的半透明渐变背景
                        "border: 4px solid #FFA500;" // 悬停时橙色边框
                    "}");
        temp2 = 0;
        QJsonObject object;
        object["beep"] = 1;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);

    }
    else
    {
        ui->pushButton_beep->setText("ON");
        ui->pushButton_beep->setStyleSheet(
                    "QPushButton {"
                    "background-color: rgba(185, 255, 185, 0.6);" // 半透明背景色
                    "color: green;"
                        "font-family: '黑体';"
                        "font-size: 28px;"
                        "border: 4px solid #FFD700;" // 添加金黄色边框
                        "border-radius: 15px;"      // 圆角半径
                        "padding: 10px;"            // 内边距
                    "}"
                    "QPushButton:hover {"
                        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
                        "stop:0 rgba(100, 255, 100, 150), stop:1 rgba(185, 255, 185, 150));" // 悬停时的半透明渐变背景
                        "border: 4px solid #FFA500;" // 悬停时橙色边框
                    "}");
        temp2 = 1;
        QJsonObject object;
        object["beep"] = 0;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
}

void SetWig::on_pushButton_fan_clicked()
{
    if(temp3 == 1)
    {
        ui->pushButton_fan->setText("OFF");
        ui->pushButton_fan->setStyleSheet(
                    "QPushButton {"
                    "background-color: rgba(185, 255, 185, 0.6);" // 半透明背景色
                    "color: rgb(255, 0, 0);"
                        "font-family: '黑体';"
                        "font-size: 28px;"
                        "border: 4px solid #FFD700;" // 添加金黄色边框
                        "border-radius: 15px;"      // 圆角半径
                        "padding: 10px;"            // 内边距
                    "}"
                    "QPushButton:hover {"
                        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
                        "stop:0 rgba(100, 255, 100, 150), stop:1 rgba(185, 255, 185, 150));" // 悬停时的半透明渐变背景
                        "border: 4px solid #FFA500;" // 悬停时橙色边框
                    "}");
        temp3 = 0;
        QJsonObject object;
        object["fan"] = 1;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);

    }
    else
    {
        ui->pushButton_fan->setText("ON");
        ui->pushButton_fan->setStyleSheet(
                    "QPushButton {"
                    "background-color: rgba(185, 255, 185, 0.6);" // 半透明背景色
                    "color: green;"
                        "font-family: '黑体';"
                        "font-size: 28px;"
                        "border: 4px solid #FFD700;" // 添加金黄色边框
                        "border-radius: 15px;"      // 圆角半径
                        "padding: 10px;"            // 内边距
                    "}"
                    "QPushButton:hover {"
                        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
                        "stop:0 rgba(100, 255, 100, 150), stop:1 rgba(185, 255, 185, 150));" // 悬停时的半透明渐变背景
                        "border: 4px solid #FFA500;" // 悬停时橙色边框
                    "}");
        temp3 = 1;
        QJsonObject object;
        object["fan"] = 0;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
}

void SetWig::updateTime()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString currentTime = currentDateTime.toString("hh:mm:ss");

    // 打印当前时间以进行调试
      //qDebug() << "Current Time:" << currentTime;

    // 将时间显示在一个 QLCDNumber 控件上
        ui->lcd_time->display(currentTime);
}
